package com.starsea.book.ui.newpage.adapter

import android.content.Context
import android.os.Bundle
import android.view.ViewGroup
import com.starsea.book.base.adapter.ItemViewHolder
import com.starsea.book.base.adapter.RecyclerAdapter
import com.starsea.book.databinding.ItemCategoryChildBinding
import com.starsea.book.ui.newpage.entity.CategoryEntity
import com.starsea.book.ui.newpage.utils.loadNormalImage
import com.starsea.book.ui.widget.recycler.ItemTouchCallback
import com.starsea.book.utils.gone

class CategoryChildAdapter(
    context: Context,
    var onChanged: ((value: CategoryEntity.CategoryChildEntity) -> Unit)? = null
) :
    RecyclerAdapter<CategoryEntity.CategoryChildEntity, ItemCategoryChildBinding>(context),
    ItemTouchCallback.Callback {


    override fun getViewBinding(parent: ViewGroup): ItemCategoryChildBinding {
        return ItemCategoryChildBinding.inflate(inflater, parent, false)
    }

    override fun registerListener(holder: ItemViewHolder, binding: ItemCategoryChildBinding) {
        binding.rllContent.setOnClickListener {
            getItem(holder.layoutPosition)?.let {
                onChanged!!.invoke(it)
            }

        }
    }

    override fun convert(
        holder: ItemViewHolder,
        binding: ItemCategoryChildBinding,
        item: CategoryEntity.CategoryChildEntity,
        payloads: MutableList<Any>
    ) {
        binding.run {
            val bundle = payloads.getOrNull(0) as? Bundle
            if (bundle == null) {
                ivCover.loadNormalImage(context, item.typePic ?: "")
                tvName.text = item.typeTitle ?: ""
//                是否热门 0:否;1:是
                rtvIsHot.gone(item.isHot == 0)

            } else {
                bundle.keySet().map {
                    when (it) {
                        "typeTitle" -> tvName.text = item.typeTitle
                        "typePic" -> ivCover.loadNormalImage(context, item.typePic ?: "")
                        "isHot" -> rtvIsHot.gone(item.isHot == 0)
                    }
                }
            }
        }
    }


}